## Set options(stringsAsFactors=FALSE in ~/.Rprofile to turn this off globally
options(stringsAsFactors = FALSE)
hmpheno = read.table("relationships_w_pops_051208.txt", header=TRUE)
smap1 = read.table("excluded_cels_sample_map.txt")
smap2 = read.table("passing_cels_sample_map.txt")
smap = rbind(smap1, smap2)
colnames(smap) = c("IID","expid")
all(smap$IID%in%hmpheno$IID)
mapdat=merge(smap, hmpheno, by="IID")

library(RPostgreSQL)
drv = dbDriver("PostgreSQL")
con = dbConnect(drv, password="pqxxrocks", dbname="affy6_faheem")
expid = data.frame(dbGetQuery(con, "SELECT expid from hapmap_shard_test.idlink"))
m = merge(expid, mapdat)
pheno = m[, c('IID', 'expid', 'FID', 'dad', 'mom', 'sex', 'population', 'pheno', 'population')]
colnames(pheno) = c("patientid", "expid", "famid", "pid", "mid", "sex", "race", "phenotype", "studyid")
phenoord = pheno[order(pheno$patientid), ]
write.csv(phenoord, file="~/snppy/data/Hapmap6.0/Pheno/hapmappheno.new.csv", quote=FALSE, row.names=FALSE)

